// Pages/cart/cart.js
import {Cart} from "./cart_model.js";
var cart = new Cart;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
     
  },
  onHide: function (){
     // 再次存入缓存
     cart.reStorageSync(this.data.cartData);
  },
  onShow: function () {
     var cartData = cart._getLocalStorageData();
     var countsInfo = cart._getCartSelectedTotalCounts(true);
     var totalCountsAndPrice = this._selectedTotalPriceAndTypeCounts(cartData); 
     this.setData({
        selectedCounts: countsInfo,
        cartData: cartData,
        selectedTypeCounts: totalCountsAndPrice.selectedTypeCounts,
        totalPrice: totalCountsAndPrice.totalPrice,
     })
  },
  //获取购物车中的总价格和种类个数
  _selectedTotalPriceAndTypeCounts:function(cartData){
     let totalPrice = 0;
     let selectedCounts = 0;
     let selectedTypeCounts = 0;
     let length = cartData.length;
     let multiple = 100;
     for(let i = 0; i < length; i++){
        if (cartData[i].selected){
           selectedCounts += cartData[i].counts;
           selectedTypeCounts++;
           totalPrice += cartData[i].counts * Number(cartData[i].price) * multiple;
        }
     }
     return {
        selectedCounts: selectedCounts,
        selectedTypeCounts: selectedTypeCounts,
        totalPrice: totalPrice/multiple
     }
  },
//   单选的时候全选也要相应改变
   toggleSelect:function(event){
      var id = cart.getDataSet(event, 'id');
      var status = cart.getDataSet(event, 'status');
      var i = this._findSelectedId(id);
      this.data.cartData[i].selected = !status;
      this._bindNewSelectedData();
   },
   // 寻找点击的id并改变其状态
   _findSelectedId:function(id){
      let data = this.data.cartData;
      let length = data.length;
      for(let i = 0; i < length; i++){
         if(data[i].id == id){
            return i;
         }
      }
   },
   // 单选后重新绑定数据
   _bindNewSelectedData:function(){
      let newData = this._selectedTotalPriceAndTypeCounts(this.data.cartData);
      this.setData({
         selectedCounts: newData.selectedCounts,
         cartData: this.data.cartData,
         selectedTypeCounts: newData.selectedTypeCounts,
         totalPrice: newData.totalPrice,
      });
   },
   // 全选中时的方法
   toggleSelectAll:function(event){
      let status = cart.getDataSet(event, 'status');
      console.log(status);
      let length = this.data.cartData.length;
      for(let i = 0; i < length; i++){
         if(status == 'true'){
            this.data.cartData[i].selected = false;
         }else{
            this.data.cartData[i].selected = true;
         }
      }
      this._bindNewSelectedData();
   },
   // 增加或减少一个商品的数量
   changeCounts:function(event){
      let id = cart.getDataSet(event, 'id');
      let type = cart.getDataSet(event, 'type');
      let length = this.data.cartData.length;
      for(let i = 0; i < length; i++){
         if(this.data.cartData[i].id == id){
            if(type == 'cut'){
               this.data.cartData[i].counts--;
            }else{
               this.data.cartData[i].counts++;
            }
            break;
         }
      }
      // 获取新计算的数据
      this._bindNewSelectedData();
   },
   // 删除商品
   delete:function(event){
      let index = cart.getDataSet(event, 'index');
      this.data.cartData.splice(index, 1);
      this._bindNewSelectedData();
   },
   // 跳转到商品祥情页
   toProductPage:function(event){
      let id = cart.getDataSet(event, 'id');
      wx.navigateTo({
         url: '../product/product?id=' + id,
      })
   },
   //跳转到支付的页面
   submitOrder:function(event){
      wx.navigateTo({
         url: '../order/order?account=' + this.data.totalPrice + '&from=cart',
      })
   },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
     wx.setNavigationBarTitle({
        title: '购物车',
     })
  },

  
})